<template>
  <div>
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="24">
          <a-col :md="8" :sm="12">
            <a-form-item label="流程编号">
              <a-input placeholder="请输入流程编号" v-model="queryParam.processDefinitionId" @pressEnter="searchQuery"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <!-- <a-form-item label="任务发起人">
              <a-input placeholder="请输入任务发起人" v-model="queryParam.userName"></a-input>
            </a-form-item>-->
            <a-form-item
              label="任务发起人"
              :labelCol="{
                xs: { span: 24 },
                sm: { span: 6 }
              }"
              :wrapperCol="{
                xs: { span: 24 },
                sm: { span: 18 }
              }"
            >
              <j-select-user-leader v-model="userInfo"></j-select-user-leader>
              <!-- <div class="showData">{{userInfo?userInfo.split(',')[0]:'请选择任务发起人'}}</div> -->
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="8">
            <span
              style="float: left;overflow: hidden;margin-top:4px"
              class="table-page-search-submitButtons"
            >
              <a-button type="primary" @click="searchQuery" icon="search" size="small">查询</a-button>
              <a-button
                type="primary"
                @click="searchReset"
                icon="reload"
                style="margin-left: 8px"
                size="small"
              >重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-dropdown>
        <a-button @click="batchReceive" size="small" type="primary" icon="audit">批量签收</a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        :loading="loading"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record" class="publicFlex">
          <template v-if="record.taskAssigneeName && record.taskAssigneeName!=''">
            <a @click="handleProcess(record)">办理</a>
            <a @click="selectEntruster(record)" style="margin-left:5px">委托</a>
            <a-popconfirm title="确定退签吗?" @confirm="() => handleWithdrawal(record)" style="margin-left:5px">
              <a>退签</a>
            </a-popconfirm>
          </template>
          <template v-else>
            <a @click="handleClaim(record)">签收</a>
          </template>
        </span>
        <!-- 字符串超长截取省略号显示-->
        <span slot="bpmBizTitle" slot-scope="text, record">
          <div style="width: 100%;text-align: left;margin-left: 5px;display: inline-block;">
            <a-icon
              title="催办提醒"
              v-if="record.taskUrge"
              theme="twoTone"
              twoToneColor="#eb2f96"
              @click="taskNotify(record)"
              type="notification"
            />
            <j-ellipsis :value="text" :length="50" />
          </div>

        </span>
      </a-table>
    </div>
    <!-- 弹出框 -->
    <task-deal-modal ref="taskDealModal" :path="path" :formData="formData" @ok="handleOk"></task-deal-modal>
    <select-entruster-modal ref="selectEntrusterModal" @selectFinished="handleEntruster"></select-entruster-modal>
    <task-notify-me-modal ref="taskNotifyMeModal"></task-notify-me-modal>
  </div>
</template>

<script>
import { filterObj } from '@/utils/util'
import { deleteAction, getAction, postAction, putAction } from '@/api/manage'
import TaskDealModal from './TaskDealModal'
import SelectEntrusterModal from './form/SelectEntrusterModal'
import JEllipsis from '@/components/jeecg/JEllipsis'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { BpmNodeInfoMixin } from '@/views/modules/bpm/mixins/BpmNodeInfoMixin'
import JSelectUserLeader from '@/components/jeecgbiz/JSelectUserLeader'
import TaskNotifyMeModal from '../../extbpm/process/TaskNotifyMeModal.vue'

export default {
  name: 'MyRunningTaskList',
  mixins: [JeecgListMixin, BpmNodeInfoMixin],
  components: {
    TaskNotifyMeModal,
    SelectEntrusterModal,
    TaskDealModal,
    JEllipsis,
    JSelectUserLeader
  },
  data() {
    return {
      userInfo: '',
      projectManagerId: '',
      description: '我的任务',
      // 查询条件
      queryParam: {},
      // 表头
      columns: [
        {
          title: '序号',
          width: 50,
          dataIndex: '',
          key: 'rowIndex',
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '业务标题',
          dataIndex: 'bpmBizTitle',
          align: 'center',
          scopedSlots: { customRender: 'bpmBizTitle' }
        },
        {
          title: '流程名称',
          align: 'center',
          dataIndex: 'processDefinitionName'
        },
        {
          title: '发起人',
          align: 'center',
          dataIndex: 'processApplyUserName'
        },
        {
          title: '开始时间',
          align: 'center',
          dataIndex: 'taskBeginTime'
        },
        {
          title: '当前环节',
          align: 'center',
          dataIndex: 'taskName'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          align: 'center',
          width: 150,
          scopedSlots: { customRender: 'action' }
        }
      ],
      path: 'modules/bpm/task/form/FormLoading',
      formData: {},
      url: {
        list: '/act/task/list',
        claim: '/act/task/claim',
        taskEntrust: '/act/task/taskEntrust',
        batchReceive: "/act/task/batchClaim",  //批量签收接口
        withDrawal: "/act/task/unclaim"  //退签接口
      },
      taskId: ''
    }
  },
  created() {
    // this.loadData()
  },
  watch: {
    userInfo: function(val) {
      console.log(val)
      this.queryParam.userName = val.split(',')[1]
      console.log(this.queryParam)
    }
  },
  methods: {
    handleProcess(record) {
      this.getProcessNodeInfo(record)
    },
    handleClaim(record) {
      var that = this
      var params = { taskId: record.id } //查询条件
      this.$confirm({
        title: '确认签收吗',
        content: '是否签收该任务?',
        onOk: function() {
          putAction(that.url.claim, params).then(res => {
            if (res.success) {
              that.$message.success(res.message)
              that.loadData()
            } else {
              that.$message.warning(res.message)
              that.loadData()
            }
          })
        }
      })
    },
    //批量签收
    batchReceive(record){
      if (!this.url.batchReceive) {
        this.$message.error('请设置url.batchReceive!')
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var taskId = ''
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          taskId += this.selectedRowKeys[a] + ','
        }
        var that = this
        this.$confirm({
          title: '确认签收',
          content: '是否签收选中数据?',
          onOk: function () {
            putAction(that.url.batchReceive, {
              taskId: taskId
            }).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadData()
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            })
          }
        })
      }
    },
    //退签
    handleWithdrawal(record){
      var that = this
      putAction(that.url.withDrawal, {taskId: record.id}).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    handleOk() {
      this.loadData()
    },
    searchReset() {
      this.queryParam = {}
      this.loadData(1)
      this.userInfo = ''
    },
    selectEntruster(record) {
      this.taskId = record.id
      this.$refs.selectEntrusterModal.select(record)
      this.$refs.selectEntrusterModal.title = '选择委托人提交流程'
    },
    handleEntruster(data) {
      var that = this
      var params = {
        taskId: this.taskId,
        taskAssignee: data.username
      } //查询条件
      console.log('委托', params)
      putAction(that.url.taskEntrust, params).then(res => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
        } else {
          that.$message.warning(res.message)
          that.loadData()
        }
      })
    },
    //催办
    taskNotify(record) {
      this.$refs.taskNotifyMeModal.notify(record)
      this.$refs.taskNotifyMeModal.title = '催办提醒'
    },

  }
}
</script>
<style lang="less" scoped>
/** Button按钮间距 */
.ant-btn {
  margin-left: 3px;
}
.showData {
  position: relative;
  width: calc(100% - 57px);
  /* top: -32px;
    left: 10px;*/
  margin-top: -30px;
  margin-left: 10px;
  height: 24px;
  line-height: 24px;
  border-radius: 5px;
  background: #f5f5f5;
  color: rgba(0, 0, 0, 0.35);
}
</style>